package cn.Y_Qiu.pet.service.impl;

import cn.Y_Qiu.basic.service.impl.BaseServiceImpl;
import cn.Y_Qiu.pet.domain.Pet;
import cn.Y_Qiu.pet.domain.PetDetail;
import cn.Y_Qiu.pet.mapper.PetDetailMapper;
import cn.Y_Qiu.pet.mapper.PetMapper;
import cn.Y_Qiu.pet.service.IPetService;
import cn.Y_Qiu.pet.util.LoginContext;
import cn.Y_Qiu.user.domain.LoginInfo;
import cn.Y_Qiu.user.domain.User;
import cn.Y_Qiu.user.mapper.LoginInfoMapper;
import cn.Y_Qiu.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailServiceImpl petDetailService;
    @Autowired
    private UserMapper userMapper;
    @Override
    @Transactional
    public void add(Pet pet) {
        // 添加pet到t_pet
        petMapper.add(pet);
        PetDetail detail = pet.getDetail();
        if (detail != null){
            detail.setPet_id(pet.getId());
            petDetailService.add(detail);
        }


    }

    @Override
    public void onSale(List<Long> ids) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("onsaleTime", new Date());
        petMapper.onsale(map);
    }

    @Override
    public void offSale(List<Long> ids) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("offsaleTime", new Date());
        petMapper.offSale(map);
    }

    @Override
    public void adpot(Long id, Long buyerId) {
        Pet pet = petMapper.queryById(id);
        User user = userMapper.queryByLoginInfoId(buyerId);
        pet.setState(0);
        pet.setUser_id(buyerId);
        pet.setOffsaletime(new Date());
        petMapper.update(pet);
    }
}
